<?php

namespace App\Http\Controllers\Api;

use App\Collect;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class CollectController extends Controller
{

    public function index(Request $request)
    {
        $orderBy = $request->order_by ? $request->order_by : 'id';
        $orderType = $request->order_type ? $request->order_type : 'desc';
        $pageSize = $request->page_size ? $request->page_size : 15;
        $lists = Collect::orderBy($orderBy, $orderType)->where('user_id', Auth::id())->with(['news'])->paginate($pageSize);
        return $lists;
    }

    public function store(Request $request)
    {
        $news_id = $request->news_id;
        if (Collect::where('user_id', Auth::id())->where('news_id', $news_id)->first()) {
            return response(['message' => '收藏成功']);
        }
        Collect::create([
            'user_id' => Auth::id(),
            'news_id' => $news_id,
        ]);
        return response(['message' => '收藏成功']);
    }

    public function delete(Request $request)
    {
        Collect::where('user_id', Auth::id())->where('news_id', $request->news_id)->delete();
        return response(['message' => '删除成功']);
    }
}
